package com.example.tao.mycar.fragment;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.tao.mycar.R;
import com.example.tao.mycar.entity.PersonDataEntity;
import com.example.tao.mycar.utils.Tools;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link HeightBarChartFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HeightBarChartFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HeightBarChartFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private BarChart mLineChart;
    private LinearLayout mNoDataView;
    private RelativeLayout mView;
    private String mLabel;
    private ArrayList<PersonDataEntity> heightTimeDataArray = new ArrayList<>();
    private final int MAX_POINT = 7;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param label Parameter 1.
     * @return A new instance of fragment HeightBarChartFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HeightBarChartFragment newInstance(String label) {
        HeightBarChartFragment fragment = new HeightBarChartFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, label);
        fragment.setArguments(args);
        return fragment;
    }

    public HeightBarChartFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mLabel = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_height_bar_chart, container, false);
        mView = (RelativeLayout)view.findViewById(R.id.height_bar_chart);
        initView(mView);
        return view;
    }

    private void initView(View view) {
        mLineChart = (BarChart) view.findViewById(R.id.bar_chart);
        mNoDataView =(LinearLayout)view.findViewById(R.id.no_data_view);
        int dataSize = heightTimeDataArray.size();
        if (dataSize == 0) {
            mLineChart.setVisibility(View.GONE);
            mNoDataView.setVisibility(View.VISIBLE);
        } else {
            mNoDataView.setVisibility(View.GONE);
            mLineChart.setVisibility(View.VISIBLE);
        }

        BarData mBarData = getBarData();

        showChart(mLineChart, mBarData, R.color.light_red_color);
    }

    private void showChart(BarChart barChart, BarData barData, int color) {
        barChart.setDrawBorders(false);
        barChart.setDescription("");
        barChart.setNoDataTextDescription("你需要为表格提供数据");

        barChart.setDrawGridBackground(false);
        barChart.setGridBackgroundColor(R.color.light_red_color);
        barChart.getXAxis().setLabelsToSkip(0);
        barChart.getAxisRight().setEnabled(false);
        barChart.getAxisRight().setDrawGridLines(false);

        barChart.getXAxis().setDrawGridLines(false);
        barChart.getXAxis().setEnabled(true);
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getXAxis().setTextColor(R.color.white_50_alpha);
        barChart.getXAxis().setAxisLineColor(R.color.white_50_alpha);

        barChart.getAxisLeft().setEnabled(false);
        barChart.getAxisLeft().setDrawGridLines(false);

        barChart.setTouchEnabled(true);
        barChart.setExtraLeftOffset(20);
        barChart.setExtraRightOffset(20);

        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(false);

        barChart.setPinchZoom(false);
        barChart.setBackgroundColor(getResources().getColor(R.color.light_red_color));

        barChart.setData(barData);

        Legend mLegend = barChart.getLegend();
        mLegend.setEnabled(false);
        mLegend.setForm(Legend.LegendForm.LINE);
        mLegend.setFormSize(2f);
        mLegend.setTextColor(Color.BLUE);
    }

    private BarData getBarData() {
        ArrayList<String> xValues = new ArrayList<>();
        ArrayList<BarEntry> yValues = new ArrayList<>();
        if (heightTimeDataArray.size() >= 0 && heightTimeDataArray.size() <= MAX_POINT) {
            for (int i = 0; i < MAX_POINT; i++) {
                if (i < heightTimeDataArray.size()) {
                    PersonDataEntity personDataEntity = heightTimeDataArray.get(i);
                    String month = Tools.getMonthFromDatePicker(personDataEntity.getTime());
                    String time = month + "-" + Tools.getDayFromDatePicker(personDataEntity.getTime());
                    xValues.add(time);
                    float height = Float.parseFloat(personDataEntity.getHeight());
                    yValues.add(new BarEntry(height, i));
                } else {
                    xValues.add("");
                    yValues.add(new BarEntry(null, i));
                }
            }
        } else if (heightTimeDataArray.size() > MAX_POINT) {
            int start = heightTimeDataArray.size() - MAX_POINT;
            for (int i = start; i <= heightTimeDataArray.size(); i++) {
                PersonDataEntity personDataEntity = heightTimeDataArray.get(i);
                String month = Tools.getMonthFromDatePicker(personDataEntity.getTime());
                String time = month + "-" + Tools.getDayFromDatePicker(personDataEntity.getTime());
                xValues.add(time);
                float height = Float.parseFloat(personDataEntity.getHeight());
                yValues.add(new BarEntry(height, i-start));
            }
        }

        BarDataSet barDataSet = new BarDataSet(yValues, mLabel);

        barDataSet.setValueTextColor(R.color.white_50_alpha);
        barDataSet.setColor(R.color.white_50_alpha);
        barDataSet.setHighLightColor(R.color.white);

        ArrayList<BarDataSet> barDataSets = new ArrayList<>();
        barDataSets.add(barDataSet);

        BarData barData = new BarData(xValues, barDataSets);
        return barData;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            //mListener.onFragmentInteraction(uri);
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        //public void onFragmentInteraction(Uri uri);
    }

    public void setHeightTimeDataArray(ArrayList<PersonDataEntity> arrayList) {
        heightTimeDataArray = arrayList;

        //BarData mBarData = getBarData();

        //showChart(mLineChart, mBarData, R.color.light_red_color);
    }

}
